<?php

	include('application/database-functions/mtbl_usuarios.php');
	include('application/database-functions/conf_constants.php');
		
	class admincp {
		
		private $mtbl_usuarios;
		private $conf_constants;
		
		function __construct() {
			
			$this->mtbl_usuarios = new Mtbl_Usuarios();
			$this->conf_constants = new Conf_Constants();
			
		}
		
		function index() {
			
			if ($_SESSION[$GLOBALS['USER_IS_LOGGED']]) {
				
				return "application/views/admincp/adminpanel.tpl";
				
			}
			
			return "application/views/admincp/index.tpl";
		}
		
		function createUser() {
			
			$tiposUsuario = $this->conf_constants->getByCategoryConf_Constants($GLOBALS['CAT_USER_TYPE'], $GLOBALS['DELETED_VALUE_FALSE'], $GLOBALS['STATUS_VALUE_ACTIVE']);
			
			if ($tiposUsuario) {
				$GLOBALS['SMARTY']->assign('tiposUsuario', $tiposUsuario);
			}
			
			return "application/views/admincp/addUser.tpl";
		}
		
		function saveUser() {
						
			if ($this->mtbl_usuarios->insertMtbl_Usuarios($_POST['sUsuario_nm'], sha1($_POST['sUsuario_psswd']), $_POST['sTUsuario_fl'], date('Y-m-d'), NULL, NULL, $GLOBALS['DELETED_VALUE_FALSE'], $GLOBALS['STATUS_VALUE_ACTIVE'], $GLOBALS['CONCURRENCY_INITIAL_VALUE'])) {

				header('Location: ' . $GLOBALS['WEB_UTILS']->get_base_url() . '/index.php?module=admincp');
				
			} else {
				
				header('Location: ' . $GLOBALS['WEB_UTILS']->get_base_url() . '/index.php?module=admincp&action=createUser');
				
			}
			
			return "";
		}
		
		function checkUser() {
			
			$userData = $this->mtbl_usuarios->getUserByUsernameAndPasswordMtbl_Usuarios($_POST['sUsuario_nm'], sha1($_POST['sUsuario_psswd']), $GLOBALS['DELETED_VALUE_FALSE'], $GLOBALS['STATUS_VALUE_ACTIVE']);
			$errorData = array();
			
			if ($this->mtbl_usuarios->getUserByUsernameMtbl_Usuarios($_POST['sUsuario_nm'], $GLOBALS['DELETED_VALUE_FALSE'], $GLOBALS['STATUS_VALUE_ACTIVE'])) {
				
				if ($userData) {
					
					if ($userData->getsTUsuario_fl() == $GLOBALS['USER_TYPE_ADMIN']) {

						$_SESSION[$GLOBALS['USER_IS_LOGGED']] = TRUE;
						$_SESSION[$GLOBALS['USER_ID']] = $userData->getlUsuario_id();
						$_SESSION[$GLOBALS['USER_NAME']] = $userData->getsUsuario_nm();
						
						header('Location: ' . $GLOBALS['WEB_UTILS']->get_base_url() . '/index.php?module=admincp');
						
					} else {
						
						array_push($errorData, $GLOBALS['MESSAGE_USER_NOT_ADMIN']);
						
					}
					
				} else {
					
					array_push($errorData, $GLOBALS['MESSAGE_USER_INVALID_PASSWORD']);
					
				}
				
			
			} else {
				
				array_push($errorData, $GLOBALS['MESSAGE_USER_DOESNT_EXISTS']);
				
			}
			
			if (sizeof($errorData) > 0) {
				
				$GLOBALS['SMARTY']->assign('error',$errorData);
				$GLOBALS['SMARTY']->assign('sUsuario_nm',$_POST['sUsuario_nm']);
				
			}
								
			return $this->index();
				
		}
		
		function logoutUser() {
			
			if ($_SESSION[$GLOBALS['USER_IS_LOGGED']]) {
				
				$_SESSION[$GLOBALS['USER_IS_LOGGED']] = FALSE;
				
			}
			
			header('Location: ' . $GLOBALS['WEB_UTILS']->get_base_url() . '/index.php?module=admincp');
			
		}
		
	}